package com.shyroke.coineye.controller;


import com.shyroke.coineye.bean.AddCoinDto;
import com.shyroke.coineye.bean.DefaultShowCoin;
import com.shyroke.coineye.bean.ShowCoinData;

import com.shyroke.coineye.service.CrawService;
import com.shyroke.coineye.service.DefaultShowCoinService;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 首页Controller
 */
@Controller
@RequestMapping(value = "/")
public class HomeController {

    Logger logger =  Logger.getLogger(getClass());

    @Autowired
    private DefaultShowCoinService defaultShowCoinService;

    @RequestMapping(value = "/index")
    public String index() throws IOException {
        logger.debug(" 访问index页面 ......");

         List<DefaultShowCoin> defaultShowCoins = defaultShowCoinService.getshowCoinList();

         List<AddCoinDto> addCoinDtos = new ArrayList<>();
        AddCoinDto addCoinDto = null;

        for(DefaultShowCoin defaultShowCoin:defaultShowCoins){
            addCoinDto = new AddCoinDto();
            BeanUtils.copyProperties(defaultShowCoin,addCoinDto);
            addCoinDtos.add(addCoinDto);
        }

        CrawService crawService = new CrawService(addCoinDtos);
        List<ShowCoinData> showCoinDataList =  crawService.parse();


        logger.debug("爬取的数据======"+showCoinDataList);
        return "index";
    }
}
